﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using D2DEngine;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using D2DEngine.Scene;
using D2DEngine.Core;
using D2DEngine.Components;
using D2DEngine.Components.AI;
using Microsoft.Xna.Framework.Graphics;
using FarseerPhysics.Dynamics;

namespace AIDev.Components
{
    public class TurretControlComponent : D2DComponent
    {
        float fireDelay = 500.0f;
        double lastFired = 0.0f;

        D2DActor actor;

        public override void OnRegister()
        {
            this.actor = this.owner as D2DActor;

            this.actor.Body.CollisionCategories = Category.Cat2;

            base.OnRegister();
        }

        
        public override void Update(GameTime gameTime)
        {
            if( (gameTime.TotalGameTime.TotalMilliseconds - lastFired) >= fireDelay)
            {
                Fire();
 
                lastFired = gameTime.TotalGameTime.TotalMilliseconds;

            }

            this.actor.Sprite.Color = Color.Black;

            

            base.Update(gameTime);
        }


        private void Fire()
        {
            D2DActor m = D2DObjectDatabase.Instance.CloneObject<D2DActor>("Projectile_missile");

            m.Position = this.actor.Position;
            m.Rotation = this.actor.Rotation;
            m.Body.CollidesWith = Category.Cat1;
            this.actor.Scene.Register(m);

        }
       
    }
}
